package com.wjj.app.fragment;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.view.View;
import android.widget.TextView;

import com.wjj.app.R;
import com.wjj.app.base.BaseFragment;

/**
 * Created by Wjj on 16/12/12.
 */
public class BaseMainFragment extends BaseFragment implements View.OnClickListener {

    private TextView tv1;
    private TextView tv2;
    private TextView tv3;

    private MainFragment1 fragment1;
    private MainFragment2 fragment2;
    private MainFragment3 fragment3;

    //对fragment管理
    private FragmentManager fragmentManager;

    public static BaseMainFragment newInstance(String msg) {
        BaseMainFragment fragment = new BaseMainFragment();
        Bundle bundle = new Bundle();
//        bundle.putSerializable(FIRST_FRAGMENT, msg);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected void initView(View view, Bundle savedInstanceState) {
        tv1 = (TextView) view.findViewById(R.id.main_tv1);
        tv2 = (TextView) view.findViewById(R.id.main_tv2);
        tv3 = (TextView) view.findViewById(R.id.main_tv3);

        fragmentManager = getActivity().getSupportFragmentManager();

        tv1.setOnClickListener(this);
        tv2.setOnClickListener(this);
        tv3.setOnClickListener(this);

        setTabSelection(1);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.main_tv1:
                setTabSelection(1);
                break;
            case R.id.main_tv2:
                setTabSelection(2);
                break;
            case R.id.main_tv3:
                setTabSelection(3);
                break;
        }
    }

    private void setTabSelection(int i) {
        clearSelection();

        // 开启一个Fragment事务
        android.support.v4.app.FragmentTransaction transaction = fragmentManager.beginTransaction();
        // 先隐藏掉所有的Fragment，以防止有多个Fragment显示在界面上的情况
        hideFragments(transaction);

//        switch (i) {
//            case 1:
//                tv1.setSelected(true);
//                if (fragment1 == null) {
//                    fragment1 = new MainFragment1();
//                    transaction.add(R.id.main_fragment_content, fragment1);
//                } else {
//                    transaction.show(fragment1);
//                }
//                break;
//            case 2:
//                tv2.setSelected(true);
//                if (fragment2 == null) {
//                    fragment2 = new MainFragment2();
//                    transaction.add(R.id.main_fragment_content, fragment2);
//                } else {
//                    transaction.show(fragment2);
//                }
//                break;
//            case 3:
//                tv3.setSelected(true);
//                if (fragment3 == null) {
//                    fragment3 = new MainFragment3();
//                    transaction.add(R.id.main_fragment_content, fragment3);
//                } else {
//                    transaction.show(fragment3);
//                }
//                break;
//        }
        transaction.commit();
    }

    private void hideFragments(android.support.v4.app.FragmentTransaction transaction) {
        if (fragment1 != null) {
            transaction.hide(fragment1);
        }
        if (fragment2 != null) {
            transaction.hide(fragment2);
        }
        if (fragment3 != null) {
            transaction.hide(fragment3);
        }
    }

    private void clearSelection() {
        tv1.setSelected(false);
        tv2.setSelected(false);
        tv3.setSelected(false);
    }
}
